package com.example.android.ui.me;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.example.android.R;
import com.example.android.bean.User;

import com.example.android.databinding.FragmentMeBinding;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.GlideEngine;
import com.example.android.utils.NetUtil;
import com.example.android.utils.SPUtils;


import java.util.Objects;

public class MeFragment extends Fragment {

    private FragmentMeBinding binding;
    private boolean isLogin;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        MeViewModel meViewModel = new ViewModelProvider(this).get(MeViewModel.class);
        binding = FragmentMeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        TextView textView = root.findViewById(R.id.textView);
        ImageView imageView = root.findViewById(R.id.profile_image);
        TextView nick = root.findViewById(R.id.textViewNick);
        TextView realName = root.findViewById(R.id.textViewRealName);
        TextView sex = root.findViewById(R.id.textViewsex2);
        //我的預約
        LinearLayout resever = root.findViewById(R.id.resever);
        resever.setOnClickListener(view -> {
            if (isLogin) {
                Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_reserveInfoFragment);
            } else {
                Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_loginFragment);
            }

        });
        //我的车辆
        LinearLayout linearLayout = root.findViewById(R.id.mycar);
        //关于我们
        LinearLayout aboutMe = root.findViewById(R.id.aboutMe);
        aboutMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
                builder.setTitle("关于我们");
                builder.setMessage("第九组 04陈立东 11黄睿 25刘咏其");
                builder.show();
            }
        });
        //获得管理员审核对象
        LinearLayout admin = root.findViewById(R.id.admin);
        admin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_reviewFragment);
            }
        });
        linearLayout.setOnClickListener(view -> {
            if (isLogin) {
                Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_frahmentCar);
            } else {
                Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_loginFragment);
            }
        });
        //我的发布
        LinearLayout releaseMePublic = root.findViewById(R.id.meReleasePublic);
        releaseMePublic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin) {
                    //我的发布页面
                    Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_releaseInfoFragment);
                } else {
                    Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_loginFragment);
                }

            }
        });

        //退出登录
        View viewById = root.findViewById(R.id.button2);
        textView.setOnClickListener(this::click);
        imageView.setOnClickListener(this::click);
        viewById.setOnClickListener(this::quit);
        String token = SPUtils.getString("token");
        Log.i("输出的token", "x" + token);
        if (token != null) {
            isLogin = true;
            NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class).getUserInfo().observe(getViewLifecycleOwner(), baseUserModel -> {
                if (baseUserModel.getCode() == 200) {
                    Log.i("输出的返回数据", baseUserModel.toString());
                    //是否显示管理员按钮
                    if (baseUserModel.getRoles().size() > 0 && baseUserModel.getRoles().get(0).equals("admin")) {
                        admin.setVisibility(View.VISIBLE);
                    }
                    User user = baseUserModel.getUser();
                    textView.setText(user.getUserName());
                    nick.setText(user.getNickName());
                    realName.setText(user.getRealName());
                    sex.setText(Objects.equals(user.getSex(), "1") ? "女" : "男");
                    viewById.setVisibility(View.VISIBLE);
                    //设置头像
                    if (user.getAvatar() != null && !user.getAvatar().equals("")) {
                        GlideEngine.createGlideEngine().loadGridImage(requireContext(), NetUtil.INSTANCE.getBaseUrlDev() + user.getAvatar(), imageView);
                    }
                } else {
                    SPUtils.putString("token", null);
                    isLogin = false;
                    viewById.setVisibility(View.GONE);

                }
            });
        } else {
            isLogin = false;
            textView.setText("点击登录");
            nick.setVisibility(View.GONE);
            realName.setVisibility(View.GONE);
            admin.setVisibility(View.GONE);
            viewById.setVisibility(View.GONE);
        }

        return root;
    }

    private void click(View view) {
        if (isLogin) {
            Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_infoFragment);
        } else {
            Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_loginFragment);
        }
    }

    private void quit(View view) {
        SPUtils.putString("token", null);
        Navigation.findNavController(view).navigate(R.id.action_navigation_me_to_loginFragment);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}